package top.abrsm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import top.abrsm.entity.AddressEntity;
import top.abrsm.mapper.AddressMapper;
import top.abrsm.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.abrsm.util.StateEnum;
import top.abrsm.util.StateException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-11-18
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, AddressEntity> implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public List<AddressEntity> queryList (AddressEntity addressEntity) {
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<> ();
        if ( addressEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getStuId () ) ) {
            queryWrapper.eq ( "stuId", addressEntity.getStuId () );
        }
        List<AddressEntity> addressEntities = baseMapper.selectList ( queryWrapper );
        return addressEntities;
    }

    @Override
    public AddressEntity saveSingle (AddressEntity addressEntity) {
        if ( addressEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( addressEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        addressEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( addressEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return addressEntity;
    }

    @Override
    public AddressEntity querySingle (AddressEntity addressEntity) {
        if ( addressEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( addressEntity.getId () ) ) {
            queryWrapper.eq ( "id", addressEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getStuId () ) ) {
            queryWrapper.eq ( "stuId", addressEntity.getStuId () );
        }
        if ( addressEntity.getIsDefault () != null ) {
            queryWrapper.eq ( "isDefault", addressEntity.getIsDefault () );
        }
        AddressEntity address = baseMapper.selectOne ( queryWrapper );
        return address;
    }

    @Override
    public AddressEntity updateSingle (AddressEntity addressEntity) {
        if ( addressEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( addressEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        UpdateWrapper<AddressEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( addressEntity.getStuId () ) ) {
            updateWrapper.set ( "isDefault", addressEntity.getIsDefault () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getReceiver () ) ) {
            updateWrapper.set ( "receiver", addressEntity.getReceiver () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getAddrName () ) ) {
            updateWrapper.set ( "addrName", addressEntity.getAddrName () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getAddr () ) ) {
            updateWrapper.set ( "addr", addressEntity.getAddr () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getMobile () ) ) {
            updateWrapper.set ( "mobile", addressEntity.getMobile () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getProvinceId () ) ) {
            updateWrapper.set ( "provinceId", addressEntity.getProvinceId () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getCityId () ) ) {
            updateWrapper.set ( "cityId", addressEntity.getCityId () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getAreaId () ) ) {
            updateWrapper.set ( "areaId", addressEntity.getAreaId () );
        }
        if ( StringUtils.isNotBlank ( addressEntity.getAreaId () ) ) {
            updateWrapper.set ( "areaId", addressEntity.getAreaId () );
        }
        addressEntity.setChangeTime ( new Date () );
        updateWrapper.eq ( "id", addressEntity.getId () );
        int update = baseMapper.update ( addressEntity, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return addressEntity;
    }

    @Override
    public AddressEntity deleteSingle (AddressEntity addressEntity) {
        int i = baseMapper.deleteById ( addressEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.DELETE_EXCE );
        }
        return addressEntity;
    }

    @Override
    public AddressEntity updateDefaultAddress (AddressEntity addressEntity) {
        Integer update = addressMapper.updateDefaultAddress ( addressEntity.getStuId () );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return addressEntity;
    }

}
